/**
 * accountmanager.js
 *
 * @author Koszti Lajos [Ajnasz] http://ajnasz.hu ajnasz@ajnasz.hu 
 * @license GPL v2
 * for more details see the license.txt file
 */

/**
 * Google account manager namespace,
 * check that the user is logged in,
 * logging in the user
 * @requires GRPrefs to get the preferences
 * @requires _passwordManager to get the users password
 * @requires #getFeedList function, to gets the feeds
 */
var accountManager = {
  // mozilla nsi cookie manager component
  CookieManager: Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager),
  /**
   * Check, that the account is configured
   * @type {Boolean}
   */
  accountExists: function() {
    if(WSPrefs.username() && passwordManager.getPassword()) {
      return true;
    }
    return false;
  },
  /**
   * @return returns the value of the cookie named `SID`
   * @type {String,Boolean}
   */
  getCurrentSID: function() 
  {
	var enumerator = this.CookieManager.enumerator;
	var rex = new RegExp('wsproject.ru$');
	while (enumerator.hasMoreElements()) 
	{
		var cookie = enumerator.getNext();
		if (cookie instanceof Components.interfaces.nsICookie) 
		{
			if (rex.test(cookie.host)) 
			{
				if(cookie.name == 'SID' && cookie) 
				{
					return true;
				}
			}
		}
	}
	return false;
  },
  /**
   * do the login into the google service
   */
  logIn: function() {
    if(this.accountExists()) {
      // var url = GRPrefs.conntype + '://www.google.com/accounts/ServiceLoginAuth';
      var url = 'https://www.google.com/accounts/ServiceLoginAuth';
      var param = 'user='+encodeURIComponent(GRPrefs.username())+'&passwd='+encodeURIComponent(passwordManager.getPassword());
      // remember the login state, possible won't ask for mozilla master password
      if(WSPrefs.rememberLogin()) {
        param += '&rememberme=1';
      }
	  Ajax.request({callback: accountManager.handleLogin,
				error: WSDataLoader.handleError,
				url: WSPref.conntype + "://" + WSPref.server + WSPref.authPath,
				request: param,
				encode: null,
				method: "post",
				charset: "windows-1251",
				typedata: "json"})
    } else {
      this.loginFailed();
      return -1;
    }
    return true;
  },
  /**
   * @param {Event} e event object
   * @returns true if the login was succes and false if wasn't
   * @type Boolean
   */
  handleLogin: function(json) 
  {
	if(json.status == "logined")
	{
		WSDataLoader.getData();
		return true;
	} else {
		accountManager.notLogined();
	}
    /*var curSid = accountManager.getCurrentSID();
    if(curSid === false) 
	{
       GRW_StatusBar.setStatus('loginerror');
       return false;
    }
    new GetList();
    return true;*/
  },
  notLogined: function ()
  {
	WSInterface.setEvent("loginerror");
    WSN_LOG('not logined ');
    return false;
  },
  /**
   * do things when the login failed
   * @returns false
   * @type Boolean
   */
  loginFailed: function(errno) 
  {
    WSInterface.setEvent("networkerror");
    WSN_LOG('login failed ('+errno+')');
    return false;
  }
};
